package com.shijiu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shijiu.annotation.RepeatSubmit;
import com.shijiu.pojo.Comment;
import com.shijiu.service.CommentService;
import com.shijiu.utils.UserThreadLocal;
import com.shijiu.vo.PageParams;
import com.shijiu.common.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author xlj
 * @Date Created in  2023/3/18 14:30
 * @DESCRIPTION:
 * @Version V1.0
 */
@RestController
@RequestMapping("comment")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class CommentController {

    @Autowired
    private CommentService commentService;
    @ApiOperation("店家申请审核")
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    @PostMapping("auditStatus")
    public Result auditStatus(@RequestBody @Validated Comment comment){
        Integer commentId = comment.getId();
        String commentStatus = comment.getStatus();
        Comment comments = new Comment();
        comments.setId(commentId);
        comments.setStatus(commentStatus);
        return Result.success(commentService.updateById(comments));
    }
    @ApiOperation("管理员评论列表-分页")
    @GetMapping("list")
    public Result getCommentList(@RequestBody PageParams pageParams){
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Comment::getShopId, UserThreadLocal.get().get("shopId")).orderByDesc(Comment::getCreateDate);
        List<Comment> list = commentService.list(lambdaQueryWrapper);
        log.info("查出来的评论列表{}",list);
        return Result.success(list);
    }
    @ApiOperation("店家评论列表-分页")
    @PostMapping("listByPage")
    public Result getCommentListByPage(@RequestBody Map<String, Object> requestBodyMaps){
        return commentService.selectOrderListByPage((Integer) UserThreadLocal.get().get("shopId"),requestBodyMaps);
    }
    @ApiOperation("店家评论列表-根据id 删除审核通过的评论")
    @PostMapping("deleteComment")
    public Result deleteComment(@RequestBody @Validated Comment comment){
        return commentService.deleteCommentById((Integer) UserThreadLocal.get().get("shopId"),comment.getId());
    }

    //添加评论 小程序添加
//    @PostMapping("api/save")
//    public Result saveComment(){
//        return Result.success("ok");
//    }

    @ApiOperation("回复评论店主")
    @PostMapping("update")
    public Result updateComment(@RequestBody Comment comment){
        comment.setIsReply(true);
        return Result.success(commentService.updateById(comment));
    }

    //删除评论  管理员删除
//    @GetMapping("admin/remove/{id}")
//    public Result removeComment(@PathVariable("id") Integer id){
//        return Result.success(commentService.removeById(id));
//    }

    //根据id搜索评论,管理员搜索
//    @GetMapping("admin/query/{id}")
//    public Result queryById(@PathVariable("id") Integer id){
//        return Result.success((commentService.getById(id)));
//    }

}
